let host = window.location.host
let pathname = window.location.pathname
var str = /^\/video/;
let flag = str.test(pathname);



if(flag){
    let video = document.querySelector('.bilibili-player-video video')
    let videoSpend = video.playbackRate
    let switchFlg = false;
    chrome.runtime.sendMessage({
        action: 'updateIcon',
        value: true
    });
    function updateIcon(){
        if (document.visibilityState=='hidden') {
            if(!chrome.app.isInstalled){
                chrome.runtime.sendMessage({
                    action: "updateIcon",
                    value: false
                })
                chrome.runtime.sendMessage({
                    action: "showInfo",
                    value: false
                })
            }
        } else if(document.visibilityState=='visible') {
            if(!chrome.app.isInstalled){
                chrome.runtime.sendMessage({
                    action: "updateIcon",
                    value: true
                })
                chrome.runtime.sendMessage({
                    action: "showInfo",
                    value: true
                })
            }
        }
    }
    document.addEventListener('visibilitychange', updateIcon)
    
    function playUp(){
        if(video.playbackRate == 0.25){
            video.playbackRate = 0.5
            ideoSpend = video.playbackRate
            chrome.runtime.sendMessage({
                action: "returnSpend",
                value: video.playbackRate
            })
            setButtonText(video.playbackRate)
            return
        }
        if(video.playbackRate >= 5){
            chrome.runtime.sendMessage({
                action: "returnSpend",
                value: video.playbackRate
            })
            setButtonText(video.playbackRate)
            return
        }
        video.playbackRate += 0.5
        videoSpend = video.playbackRate
        chrome.runtime.sendMessage({
            action: "returnSpend",
            value: video.playbackRate
        })
        setButtonText(video.playbackRate)
    }
    function playDown(){
        if(video.playbackRate <= 0.5){
            video.playbackRate = 0.25
            videoSpend = video.playbackRate
            chrome.runtime.sendMessage({
                action: "returnSpend",
                value: video.playbackRate
            })
            setButtonText(video.playbackRate)
            return
        }
        video.playbackRate -= 0.5
        videoSpend = video.playbackRate
        chrome.runtime.sendMessage({
            action: "returnSpend",
            value: video.playbackRate
        })
        setButtonText(video.playbackRate)
    }

    function resetSpend(){
        video.playbackRate = 1
        videoSpend = video.playbackRate
        chrome.runtime.sendMessage({
            action: "returnSpend",
            value: video.playbackRate
        })
        setButtonText(video.playbackRate)
    }
    
    function autoChange(){
        video.playbackRate = videoSpend
        setButtonText(video.playbackRate)
    }

    function addautoChange(){
        switchFlg = true
        video.addEventListener('loadstart', autoChange)
    }

    function removeChange(){
        switchFlg = false
        video.removeEventListener('loadstart',autoChange)
    }

    function setSpend(spend){
        videoSpend = spend;
        setButtonText(video.playbackRate)
    }

    function setButtonText(spend){
        document.querySelector('.bilibili-player-video-btn-speed-name').innerHTML = spend + 'X'
    }

    window.addEventListener('keydown', e =>{
        if(e.keyCode == 88){
            playDown()
        }else if(e.keyCode == 67){
            playUp()
        }else if(e.keyCode == 90){
            resetSpend()
        }else if(e.keyCode == 78){
            let body = document.querySelector('.bilibili-player-dm-tip-wrap')
            let hitNode = document.createElement('div')
            hitNode.setAttribute('id','hit-next')
            hitNode.style.cssText = `position: fixed;
            top:50%;
            left:50%;
            z-index: 999999;
            background-color: #fb7299;
            color: #fff;
            font-size: 12px;
            padding: 10px 25px;
            border-radius: 10px;
            transform: translateY(-50%) translateX(-50%);`
            if(switchFlg){
                hitNode.innerText = '您已关闭自动下一集调节速度'
                body.appendChild(hitNode)
                setTimeout(()=>{
                    body.removeChild(hitNode)
                },2000)
                removeChange()
            }else{
                hitNode.innerText = '您已开启自动下一集调节速度'
                body.appendChild(hitNode)
                setTimeout(()=>{
                    body.removeChild(hitNode)
                },2000)
                addautoChange()
            }
        }
    })

    function getVideoInfo(){
        let hitElement = document.querySelector('#viewbox_report .tit')
        let titleElement = document.querySelector('#multi_page .watched.on .part')
        let durationElement = document.querySelector('#multi_page .watched.on .duration')
        let timeElement = document.querySelector('.bilibili-player-video-time-total')
        let infoObj = {
            tit: hitElement ? hitElement.textContent : '',
            title: titleElement ? titleElement.textContent : "",
            duration: durationElement ? durationElement.textContent : '',
            time: timeElement ? timeElement.textContent : '',
            spend: video.playbackRate,
            switchFlg:switchFlg
        }
        chrome.runtime.sendMessage({
            action: 'backInfo',
            value: infoObj
        });
    }
}